<?

class Debug
{
	private static $time_queue = array();
	
	public static function info($_info)
	{
        if(!develop) return;
        
        error_log(date("[Y-m-d H:i:s]") . $_info ."\n", 3, path_prefix_application . 'log/debug_info.log');
    }
    
    public static function error($_exception)
    {
        $trace = $_exception->getTrace();
        $exception_str = $_exception->getMessage() . "<br>" . "\n";
        foreach($trace as $val)
        {
            $exception_str .=  '[' . (empty($val['class'])?'':$val['class']) . (empty($val['type'])?'':$val['type']) . $val['function'] . '] ' . $val['file'] . " on line " . $val['line'] . "<br>" . "\n";
        }
        
        if(!develop)
        {
            error_log(date("[Y-m-d H:i:s]") ."\n" . $exception_str ."\n", 3, path_prefix_application . 'log/error_log_' . date('Ymd') . '.log');
        }
        else
        {
            echo $exception_str;
        }
    }
	
	public static function time($_time_name)
	{
        if(!develop) return;
		
		Debug::$time_queue[$_time_name]['start_time'] = Debug::get_microtime();
		
		Debug::info($_time_name . " 运行时间计时开始");
	}
	
	public static function timeEnd($_time_name)
	{
        if(!develop) return;
		
		Debug::$time_queue[$_time_name]['stop_time'] = Debug::get_microtime();
		
		Debug::info($_time_name . " 运行时间计时结束，用时 " . round((Debug::$time_queue[$_time_name]['stop_time'] - Debug::$time_queue[$_time_name]['start_time']), 5));
		
		unset(Debug::$time_queue[$_time_name]);
	}
	
	private static function get_microtime()
    {
        list($usec, $sec) = explode(' ', microtime());
        return ((float)$usec + (float)$sec);
    }
    
    public static function memoryUsage()
    {
		Debug::info("运行内存：" . memory_get_usage());	
    }
}
?>